package cn.lingyangwl.agile.pay.client.manager;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.pay.client.constants.OrderStatus;
import cn.lingyangwl.agile.pay.client.constants.PayStatusEnum;
import cn.lingyangwl.agile.pay.client.mapper.GoodsOrderMapper;
import cn.lingyangwl.agile.pay.client.model.rqrs.ConfirmOrderRQ;
import cn.lingyangwl.agile.pay.client.model.entity.GoodsInfo;
import cn.lingyangwl.agile.pay.client.model.entity.GoodsOrder;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author shenguangyang
 */
@Component
public class GoodsOrderManager extends ServiceImpl<GoodsOrderMapper, GoodsOrder> {

    public GoodsOrder saveGoodsOrder(GoodsInfo goodsInfo, ConfirmOrderRQ req, String mchOrderNo) {
        GoodsOrder po = new GoodsOrder();
        // 设置商品信息
        po.setGoodsId(goodsInfo.getId());
        po.setGoodsTitle(goodsInfo.getTitle());
        po.setGoodsAmount(req.getPayAmount());
        po.setPayAmount(req.getPayAmount());

        // 设置用户信息
        po.setUserId(SecurityUtils.getUserId());
        po.setNickname(SecurityUtils.getUserName());

        // 设置订单信息
        po.setMchOrderNo(mchOrderNo);
        po.setDel("0");
        po.setBuyNum(req.getBuyNum());
        po.setPayAmount(req.getPayAmount());
        // 总价: 没有使用优惠卷
        po.setTotalAmount(req.getPayAmount());
        po.setStatus(PayStatusEnum.NOT_PAY.getCode());
        po.setWayCode(req.getWayCode());

        // 设置发票信息
        po.setBillType("1");
        po.setBillHeader(req.getBillHeader());
        po.setBillReceiverEmail(req.getBillReceiverEmail());
        po.setBillReceiverPhone(req.getBillReceiverPhone());
        po.setBillContent(req.getBillContent());

        this.save(po);
        return po;
    }


    public GoodsOrder getByMchOrderNo(String mchOrderNo, Long userId) {
        return this.getOne(GoodsOrder.lqw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId)
                .eq(GoodsOrder::getMchOrderNo, mchOrderNo));
    }


    public void updateNoPayToClose(String mchOrderNo, Long userId) {
        LambdaUpdateWrapper<GoodsOrder> luw = GoodsOrder.luw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId).eq(GoodsOrder::getMchOrderNo, mchOrderNo)
                .eq(GoodsOrder::getStatus, OrderStatus.NO_PAY.getCode())
                .set(GoodsOrder::getStatus, OrderStatus.CLOSED.getCode());
        this.update(luw);
    }

    public void updateNoPayToSuccess(String mchOrderNo, Long userId) {
        LambdaUpdateWrapper<GoodsOrder> luw = GoodsOrder.luw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId)
                .eq(GoodsOrder::getMchOrderNo, mchOrderNo)
                .eq(GoodsOrder::getStatus, OrderStatus.NO_PAY.getCode())
                .set(GoodsOrder::getStatus, OrderStatus.SUCCESS.getCode());
        this.update(luw);
    }

    public void updateNoPayToCancel(String mchOrderNo, Long userId) {
        LambdaUpdateWrapper<GoodsOrder> luw = GoodsOrder.luw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId)
                .eq(GoodsOrder::getMchOrderNo, mchOrderNo)
                .eq(GoodsOrder::getStatus, OrderStatus.NO_PAY.getCode())
                .set(GoodsOrder::getStatus, OrderStatus.CANCEL.getCode());
        this.update(luw);
    }

    public void updateSuccessToRefunding(String mchOrderNo, Long userId) {
        LambdaUpdateWrapper<GoodsOrder> luw = GoodsOrder.luw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId)
                .eq(GoodsOrder::getMchOrderNo, mchOrderNo)
                .eq(GoodsOrder::getStatus, OrderStatus.SUCCESS.getCode())
                .set(GoodsOrder::getStatus, OrderStatus.REFUND_PROCESSING.getCode());
        this.update(luw);
    }

    public void updateRefundingToOther(String mchOrderNo, Long userId, OrderStatus orderStatus) {
        LambdaUpdateWrapper<GoodsOrder> luw = GoodsOrder.luw()
                .eq(Objects.nonNull(userId), GoodsOrder::getUserId, userId)
                .eq(GoodsOrder::getMchOrderNo, mchOrderNo)
                .eq(GoodsOrder::getStatus, OrderStatus.REFUND_PROCESSING.getCode())
                .set(GoodsOrder::getStatus, orderStatus.getCode());
        this.update(luw);
    }
}
